package com.vieewer.btdemo.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import com.vieewer.btdemo.R;
import com.vieewer.btdemo.bean.HomMenuBean;
import com.vieewer.btdemo.view.info.SugonGlassesInfoManager;
import com.vieewer.btdemo.widget.MyProgressBar;

import java.util.List;

/**
 * ViewPager适配器
 *
 */

public class ViewPagerAdapter extends PagerAdapter {

    private Context mContext;
    private List<HomMenuBean> mData;

    public ViewPagerAdapter(Context context, List<HomMenuBean> images) {
        this.mContext = context;
        this.mData = images;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Log.i("yjj", "vp position =" + position);

        View view = null;
        if (position == 0) {
            view = View.inflate(mContext, R.layout.vp_item_sugon_glasses_info, null);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            view.setLayoutParams(params);
            setSugonInfoViews(view, position);
            container.addView(view);
        } else {
            view = View.inflate(mContext, R.layout.view_page_item, null);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            view.setLayoutParams(params);
            //view.setLayoutParams(new ViewGroup.LayoutParams(DensityUtil.px2dp(512),DensityUtil.px2dp(228))); //512*228

            TextView title = view.findViewById(R.id.txt_setting);
            title.setText(mData.get(position).titleName);
            ImageView iconImg = view.findViewById(R.id.img_setting);
            iconImg.setImageResource(mData.get(position).iconResId);
            container.addView(view);
        }

        return view;
    }

    private void setSugonInfoViews(View view, int pos) {
        TextView txt_current_time = view.findViewById(R.id.txt_current_time);
        TextView txt_current_hour = view.findViewById(R.id.txt_current_hour);
        TextView txt_compass_direction = view.findViewById(R.id.txt_compass_direction);
        ImageView img_compass = view.findViewById(R.id.img_compass);


        MyProgressBar smallSeekBar = view.findViewById(R.id.sb_charge_small_battery);
        TextView txt_inner_battery = view.findViewById(R.id.txt_inner_battery);
        MyProgressBar seekBar = view.findViewById(R.id.sb_charge_battery);
        TextView txt_left_time = view.findViewById(R.id.txt_left_time);

        txt_current_time.setText(SugonGlassesInfoManager.Companion.getInstance().getCurrentTime());
        txt_current_hour.setText(SugonGlassesInfoManager.Companion.getInstance().getCurrentHour());

        int innerValue = SugonGlassesInfoManager.Companion.getInstance().getBatteryInfos();
        txt_inner_battery.setText(innerValue + "%");
        smallSeekBar.setProgress(innerValue);

        txt_compass_direction.setText(mData.get(pos).titleName);

    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}
